package com.wsc.wisdomchina.activitity.samemodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.InfoListBean;
import com.wsc.wisdomchina.javaBean.RespondObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

/**
 * 非列表详情
 * Created by ${wangtao} on 2017/3/2.
 */

public class InfoModuleDetailActivity extends BaseActivity implements View.OnClickListener,ContributorView{

    private Context context;
    private TextView fragment_webView_title;
    private ImageView fragment_webView_back;
    private WebView fragment_webView_webView;
    private String id;
    private ContributorPresenter contributorPresenter;
    private final String PAGEDETAILFLAG="PAGEDETAILFLAG";
    private ProgressBar pb_newsdetail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pageinfodetail_layout);
        initView();
        context=this;
        Intent intent=getIntent();
        id=intent.getStringExtra("id");
        String name=intent.getStringExtra("name");
        fragment_webView_title.setText(name);

        contributorPresenter=new ContributorPresenter(this,context);
        contributorPresenter.getInformationDetail(id,PAGEDETAILFLAG);
    }

    private void initView()
    {
        fragment_webView_title=(TextView) findViewById(R.id.tv_headtitle);
        fragment_webView_back=(ImageView) findViewById(R.id.regist_back);
        fragment_webView_webView=(WebView) findViewById(R.id.fragment_webView_webView);
        fragment_webView_webView.getSettings().setDefaultTextEncodingName("UTF-8");
        fragment_webView_webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        fragment_webView_webView.getSettings().setLoadWithOverviewMode(false);
        fragment_webView_back.setOnClickListener(this);
        pb_newsdetail = (ProgressBar) findViewById(R.id.pb_newsdetail);
        pb_newsdetail.setVisibility(View.VISIBLE);
        fragment_webView_webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pb_newsdetail.setProgress(newProgress);
                pb_newsdetail.postInvalidate();
                if (newProgress == 100) {
                    pb_newsdetail.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                // TODO Auto-generated method stub
                super.onReceivedTitle(view, title);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(PAGEDETAILFLAG)) {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object message=respObj.getObj();
                if (code.equals("200") && message!=null) {
                    InfoListBean newsDetailBean=JsonTools.jsonStringToObject(JsonTools.objectToJsonString(message),InfoListBean.class);
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    fragment_webView_webView.loadData(Url, "text/html; charset=UTF-8", null);
                } else {
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

}
